package com.sdehua.wear.service.impl;

import com.sdehua.wear.entity.Collect;
import com.sdehua.wear.dao.CollectDao;
import com.sdehua.wear.entity.CollectVO;
import com.sdehua.wear.service.CollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Collect)表服务实现类
 *
 * @author makejava
 * @since 2021-12-31 15:28:27
 */
@Service("collectService")
public class CollectServiceImpl implements CollectService {
    @Resource
    private CollectDao collectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Collect queryById(Integer id) {
        Collect collect = this.collectDao.queryById(id);
        return collect;
    }

    /**
     * @MethodName deleteFavorite
     * @Author coffee
     * @Description 取消收藏
     * @Date 15:54 2022/1/1
     * @Param [userId, goodsId]
     * @return boolean
     **/
    @Override
    public boolean deleteFavorite(String userId, String goodsId) {
        Collect collect = queryOne(userId, goodsId);
        int i = collectDao.deleteById(collect.getId());
        if (i<1) return false;
        return true;
    }

    @Override
    public Collect queryOne(String userId, String goodsId) {
        Collect collect = new Collect();
        collect.setUserId(Integer.parseInt(userId));
        collect.setGoodsId(goodsId);
        List<Collect> collects = collectDao.queryAll(collect);
        if (collects.size()<1){
            return null;
        }
        return collects.get(0);
    }



    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Collect> queryAllByLimit(int offset, int limit) {
        return this.collectDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param collect 实例对象
     * @return 实例对象
     */
    @Override
    public Collect insert(Collect collect) {
        this.collectDao.insert(collect);
        return collect;
    }

    /**
     * 修改数据
     *
     * @param collect 实例对象
     * @return 实例对象
     */
    @Override
    public Collect update(Collect collect) {
        this.collectDao.update(collect);
        return this.queryById(collect.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.collectDao.deleteById(id) > 0;
    }

    @Override
    public Collect setFavorite(String userId, String goodsId) {
        Collect collect = new Collect();
        collect.setUserId(Integer.parseInt(userId));
        collect.setGoodsId(goodsId);
        List<Collect> collects = collectDao.queryAll(collect);
        if (collects.size()>0) {
            collect.setGoodsId("-1");
            collect.setUserId(-1);
            return collect;
        }
        int insert = collectDao.insert(collect);
        if (insert<1)return null;
        return collect;
    }

    @Override
    public List<CollectVO> queryAll(String userId) {
        if (userId.isEmpty())return null;
        List<CollectVO> list = collectDao.queryAllByUserId(userId);
        if (list.size() < 1) return null;
        return list;
    }
}